/*
 * @Author: weidewei
 * @Date: 2025-05-19 09:09:42
 * @LastEditors: weidewei
 * @LastEditTime: 2025-05-21 17:49:58
 * @Description:
 * @FilePath: \v3\vite.config.ts
 */
import { defineConfig } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import viteCompression from "vite-plugin-compression";
import vue from "@vitejs/plugin-vue";
// import { visualizer } from 'rollup-plugin-visualizer'
import path from "path";
import MarkdownPlugin from "./src/plugins/vite-plugin-md";
// @ts-expect-error
import eslintPlugin from "vite-plugin-eslint";

// https://vite.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		// eslintPlugin({
		// 	include: ["src/**/*.ts", "src/**/*.vue", "src/**/*.tsx", "src/**/*.jsx"]
		// }),
		AutoImport({
			resolvers: [ElementPlusResolver({ importStyle: "css" })], // 关键配置
			imports: ["vue", "vue-router"], // 自动导入 Vue 相关函数
			dts: true // 生成 auto-imports.d.ts 类型声明文件
		}),
		Components({
			resolvers: [ElementPlusResolver({ importStyle: "css" })] // 关键配置
		}),
		// viteCompression({
		//   verbose: true, // 输出压缩日志
		//   disable: false, // 是否强制启用压缩
		//   threshold: 10240,  // 对超过10kb的文件进行压缩
		//   algorithm: 'gzip',// 压缩算法
		//   ext: '.gz', // 生成的压缩包后缀
		// }),
		viteCompression({
			verbose: true,
			disable: false,
			threshold: 10240,
			algorithm: "brotliCompress",
			ext: ".br"
		}),
		MarkdownPlugin()
		// visualizer({
		//   filename: 'report.html',
		//   open: true,
		//   gzipSize: true,
		//   brotliSize: true
		// })
	],
	resolve: {
		alias: {
			"@": path.resolve(__dirname, "./src"),
			"#": path.resolve(__dirname, "./types"),
			"element-plus/es": path.resolve(__dirname, "node_modules/element-plus/lib")
		}
	},
	// vite.config.ts
	server: {
		proxy: {
			"/api": {
				target: "http://shanhe.kim/api/za/tianqi.php",
				changeOrigin: true,
				rewrite: path => path.replace(/^\/api/, "") // 移除多余层级
			}
		}
	},
	build: {
		rollupOptions: {
			output: {
				chunkFileNames: "js/[hash].js", // 引入文件名的名称
				entryFileNames: "js/[hash].js", // 包的入口文件名称
				assetFileNames: "[ext]/[hash].[ext]", // 资源文件像 字体，图片等
				// manualChunks: {
				//   'element-plus': ['element-plus'],
				//   'vue-router': ['vue-router'],
				//   'axios': ['axios'],
				//   'pinia': ['pinia'],
				//   'pinia-plugin-persistedstate': ['pinia-plugin-persistedstate'],
				//   'vue': ['vue']
				// }
				manualChunks(id) {
					if (id.includes("node_modules")) {
						return "vendor-node_modules";
					}
				}
			}
		}
	}
});
